<?php
/**
 * 结账Model
 * @author solar
 */
class CheckoutModel extends BaseModel 
{
	public static $name='checkout';
	public static $primary='c_id';
	
	public function __construct() {
		parent::__construct(CoreFactory::db(),self::$name,self::$primary);
	}
	
	/**
	 * 新建一条结账记录
	 * @author solar
	 * @param int $group_id
	 * @return boolean
	 */
	public function newCheckout($group_id) {
		return $this->insert(array('g_id'=>$group_id));
	}
	
	/**
	 * 账单总额累加
	 * @author solar
	 * @param int $c_id
	 * @param float $amount
	 * @return int
	 */
	public function increaseTotal($c_id,$amount) {
		$this->db->query('UPDATE `checkout` SET c_total=c_total+'.$amount.' WHERE c_id='.$c_id.' LIMIT 1');
		return $this->db->affectedRows(); 
	}
	
	/**
	 * 未结账单
	 * @author solar
	 * @param int $group_id
	 * @return array
	 */
	public function getUncheckout($group_id) {
		return $this->getByField(array('g_id'=>$group_id,'c_status'=>0));
	}
	
	/**
	 * 状态改为已结账
	 * @author solar
	 * @param int $checkout_id
	 * @return boolean
	 */
	public function close($checkout_id) {
		$row['c_creator']=CoreFactory::session()->get('user_id');
		$row['c_close_time']=time();
		$row['c_status']=1;
		return $this->update($row, $checkout_id);
	}
}

?>